var superagent = require('superagent');
var cheerio = require('cheerio');
var eventproxy = require('eventproxy');
var url = require('url');

var cnodeUrl = 'https://cnodejs.org/';

// 用 superagent 去抓取 https://cnodejs.org/ 的内容
superagent.get(cnodeUrl).end(function (err, res) {
        // 常规的错误处理
        if (err) {
            return next(err);
        }
        var topicUrls = [];
        var $ = cheerio.load(res.text);
        // 获取首页所有的链接
        $('#topic_list .topic_title').each(function (idx, element) {
            var $element = $(element);
            var href = url.resolve(cnodeUrl, $element.attr('href'));
            topicUrls.push(href);
       });

        var ep = new eventproxy();

        // 命令 ep 重复监听 topicUrls.length 次 `topic_html` 事件再行动
        ep.after('topic_html', topicUrls.length, function (topics) {
            // topics 是个数组, 包含了40次ep.emit('topic_html', pair) 中的那40个pair

            // 开始行动
            var noTitleAndComment = [];
            var onlyTitle = [];
            var onlyComment = [];
            topics = topics.map(function (topicPair, i) {
                // 接下来都是jquery 的用法了
                var topicUrl = topicPair[0];
                var topicHtml = topicPair[1];
                var topicIndex = topicPair[2];

                var $ = cheerio.load(topicHtml);

                var title = $('.topic_full_title').text().trim();
                var comment = $('.reply_content').eq(0).text().trim();
                if(title == '' && comment == ''){
                    topics.splice(i, 1);
                    noTitleAndComment.push(topicIndex);
                }else if(title == ''){
                    topics.splice(i, 1);
                    onlyComment.push(topicIndex);
                    return ({
                        index: topicIndex,
                        title: title,
                        href: topicUrl,
                        comment1: comment,
                    });
                }else if(comment == ''){
                    topics.splice(i, 1);
                    onlyTitle.push(topicIndex);
                    return ({
                        index: topicIndex,
                        title: title,
                        href: topicUrl,
                        comment1: comment,
                    });
                }else{
                    return ({
                        index: topicIndex,
                        title: title,
                        href: topicUrl,
                        comment1: comment,
                    });
                }
            });

            console.log('final:');
            console.log(topics);
            noTitleAndComment.length>0 && console.log('noTitleAndComment:'+noTitleAndComment);
            onlyTitle.length>0 && console.log('onlyTitle:'+onlyTitle);
            onlyComment.length>0 && console.log('onlyComment:'+onlyComment);

        })
        
        // topicUrls.forEach(function (topicUrl, index) {
        //     superagent.get(topicUrl)
        //         .end(function (err, res) {
        //             console.log(index + ' fetch ' + topicUrl + ' successful');
        //             ep.emit('topic_html', [topicUrl, res.text, index]);
        //         });
        // });


        //改造成1秒请求1个链接
        function loadOneLink(url, index) {
            superagent.get(url).end(function (err, res) {
                console.log(index + ' fetch ' + url + ' successful');
                ep.emit('topic_html', [url, res.text, index]);
                if(index == topicUrls.length -1) return;
                setTimeout(function (args) {
                    loadOneLink(topicUrls[index+1], index+1);
                }, 1000);
            })
        }
        loadOneLink(topicUrls[0], 0);

    });